const { User } = require('../model')

// 用户注册
exports.register = async (req, res, next) => {
  try {
    let user = new User(req.body.user)

    await user.save()

    user = user.toJSON()
    delete user.password

    res.status(201).json({
      user
    })
  } catch (err) {
    next(err)
  }
}

// 用户登录
exports.login = async (req, res, next) => {
  try {
    await res.send('登录成功')
  } catch (err) {
    next(err)
  }
}

// 用户查询
exports.user = async (req, res, next) => {
  try {
    const email = req.url.replace('/', '')
    let user = await User.findOne({ email })

    user = user.toJSON()
    delete user.password

    res.status(200).json({
      user
    })
  } catch (err) {
    next(err)
  }
}